/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.benefitj.consistenthash;

import java.util.Arrays;

/**
 * a sample usage for routing a request to services based on requester ip
 */
public class MyServiceNode implements Node {
  private final String idc;
  private final String ip;
  private final int port;

  private final String address;
  private final String key;

  public MyServiceNode(String idc, String ip, int port) {
    this.idc = idc;
    this.ip = ip;
    this.port = port;

    this.address = ip + ":" + port;
    this.key = idc + "-" + address;
  }

  @Override
  public String getNodeAddress() {
    return address;
  }

  @Override
  public String getKey() {
    return key;
  }

  @Override
  public String toString() {
    return getKey();
  }

  public static void main(String[] args) {
    //initialize 4 service node
    MyServiceNode node1 = new MyServiceNode("IDC1", "127.0.0.1", 8080);
    MyServiceNode node2 = new MyServiceNode("IDC1", "127.0.0.1", 8081);
    MyServiceNode node3 = new MyServiceNode("IDC1", "127.0.0.1", 8082);
    MyServiceNode node4 = new MyServiceNode("IDC1", "127.0.0.1", 8084);

    //hash them to hash ring
    ConsistentHashRouter<MyServiceNode> consistentHashRouter = new ConsistentHashRouter<>();
    //10 virtual node
    consistentHashRouter.addNodes(Arrays.asList(node1, node2, node3, node4), 10);

    //we have 5 requester ip, we are trying them to route to one service node
    String requestIP1 = "192.168.0.1";
    String requestIP2 = "192.168.0.2";
    String requestIP3 = "192.168.0.3";
    String requestIP4 = "192.168.0.4";
    String requestIP5 = "192.168.0.5";

    goRoute(consistentHashRouter, requestIP1, requestIP2, requestIP3, requestIP4, requestIP5);

    MyServiceNode node5 = new MyServiceNode("IDC2", "127.0.0.1", 8080);//put new service online
    System.out.println("-------------putting new node online " + node5.getKey() + "------------");
    consistentHashRouter.addNode(node5, 5);

    goRoute(consistentHashRouter, requestIP1, requestIP2, requestIP3, requestIP4, requestIP5);

    consistentHashRouter.removeNode(node3);
    System.out.println("-------------remove node online " + node3.getKey() + "------------");
    goRoute(consistentHashRouter, requestIP1, requestIP2, requestIP3, requestIP4, requestIP5);


  }

  private static void goRoute(ConsistentHashRouter<MyServiceNode> consistentHashRouter, String... requestIps) {
    for (String requestIp : requestIps) {
      System.out.println(requestIp + " is route to " + consistentHashRouter.routeNode(requestIp));
    }
  }
}
